<?php
require_once("initialize.php");

class StatsController{
    
    private $user;
    private $cp;
    
    public function __construct() {
        
        $this->cp   = new Company_Product();
        $this->user = new User();
    }
    
   // Method finds the male and female percentage of all users
     public static function getSexPercentage(){
 
 	       
                // Gets table with the number of females and males. Adds them togehter and gets the total sum of users
 	       $tab             =  User::sortbySex(User::find_all());
 	       $male            = $tab[0];
 	       $female          = $tab[1];
 	       $total           = $male + $female;
 
 	       // Gets the percentage of males and females.
 	       $femalepercent   = ($female/$total) * 100;
 	       $malepercent     = 100 - $femalepercent;
 
 
 
 	       //Formats the numbers, they are now containing one decimal.
 	       $femalepercent   = new StatsItem("Female", self::formatnumber($femalepercent));
 	       $malepercent     = new StatsItem("Male", self::formatnumber($malepercent));
 	       $array[] = $femalepercent;
 	       $array[] = $malepercent;
 
 	       return $array;
 
 	    }
            
            // Returns an array of views over the last $days
   public function getViewData($days, $con_id){
       
       return $this->cp->getViewsStat($days, $con_id);
   }
            
            // Returns array with the amount of males and females visited the connection_id
   public function getSexPercentageOnID($con_id)
   {
       $male    = $this->user->findSexByConid($con_id, "male");
       $female  = $this->user->findSexByConid($con_id, "female");
       $total   = $male + $female;
       
       $m = new StatsItem("Male", $male/$total);
       $f = new StatsItem("Female", $female/$total);
       
       $array[] = $m;
       $array[] = $f;
       
       return $array;
       
   }

   // Converts table of StatsItems to a Json table
   public function convertToJson($tab){
       
       $c = count($tab);
       
       for($i=0; $i<$c; $i++){
           $stringtab   = null;
           $stringtab[] = $tab[$i]->getName();
           $stringtab[] = (double)$tab[$i]->getValue();
           $newtab[]    = $stringtab;
       }
       
       $jsontab=  json_encode($newtab);
       return $jsontab;
   }
   
   // Gets the average rate by sex and age and returns an array for javascript
   public function getAgeToSexRating($con_id=0){
       $user    = new User();
       $rate    = new Rate();
       $m       = 'male';
       $f       = 'female';
        
       $tab1 =  $rate->getAvgRateByAge(0, 18, $con_id, $m);
       $tab2 =  $rate->getAvgRateByAge(0, 18, $con_id, $f);
       
       $tab3 =  $rate->getAvgRateByAge(18, 29, $con_id, $m);
       $tab4 =  $rate->getAvgRateByAge(18, 29, $con_id, $f);
       
       $tab5 =  $rate->getAvgRateByAge(29, 44, $con_id, $m);
       $tab6 =  $rate->getAvgRateByAge(29, 44, $con_id, $f);
       
       $tab7 =  $rate->getAvgRateByAge(44, 99, $con_id, $m);
       $tab8 =  $rate->getAvgRateByAge(44, 99, $con_id, $f);
       
       $array = array($tab1,$tab2 ,$tab3,$tab4, $tab5,$tab6, $tab7,$tab8);
       
       return ($this->javascriptArray($array));
   }
   
   // Recieves an array and prepares it for javascript
   public function javascriptArray($array){
       $newarray= "";

       for($i=0;$i<count($array);$i++){
           
           $newarray .= "[".$array[$i]."],";
           
       }
       return $newarray;
   }
   
   //Recieves rate from user and saves it in database
   public function rateReceiver($ratec=0,$userid=0,$connid=0){
      
       if($ratec!=0 && $userid!=0 && $connid!=0){
        $rate = new Rate();
        $rate->value = $ratec;
        $rate->connection_id = $connid;
        $rate->user_id = $userid;
        $rate->timestamp = strftime("%Y-%m-%d %H:%M:%S", time());
        $rate->save(); 
       }else{
           return "feil, ta kontakt med administrator";
       }
   }
   
   // Saves the motion of an actual user id
   public function setMotion($userid,$connid){
       $motion = new Motion();
       $motion->user_id = $userid;
       $motion->company_product_id = $connid;
       $motion->timestamp =strftime("%Y-%m-%d %H:%M:%S", time());
       $motion->save();
   }
   
   // Returns an javascript array with the count of users by sex and age
   public function getAgeToSexCount($con_id=0){
       $user    = new User();
       $rate    = new Rate();
       $m       = 'male';
       $f       = 'female';
        
       $tab1 =  $rate->getCountBySexAge(0, 18, $con_id, $m);
       $tab2 =  $rate->getCountBySexAge(0, 18, $con_id, $f);
       
       $tab3 =  $rate->getCountBySexAge(18, 29, $con_id, $m);
       $tab4 =  $rate->getCountBySexAge(18, 29, $con_id, $f);
       
       $tab5 =  $rate->getCountBySexAge(29, 44, $con_id, $m);
       $tab6 =  $rate->getCountBySexAge(29, 44, $con_id, $f);
       
       $tab7 =  $rate->getCountBySexAge(44, 99, $con_id, $m);
       $tab8 =  $rate->getCountBySexAge(44, 99, $con_id, $f);
       
       $array = array($tab1,$tab2 ,$tab3,$tab4, $tab5,$tab6, $tab7,$tab8);
       
       return ($this->javascriptArray($array));
       
   }
   
   // Recieves likes on a comment_id and saves it in the database
   public function likeReceiver($like,$commentid){
      
      $comment = comment::find_by_id($commentid);
      
      if($like == 1){
          $comment->likes ++;
      }else{
          $comment->dislikes ++;
      }
      $comment->save();
       
   }
}
$stats_controller = new StatsController();
?>
